package com.xc.ezplan.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xc.ezplan.admin.entity.AdminDept;
import com.xc.ezplan.admin.service.AdminDeptService;
import com.xc.ezplan.admin.vo.AdminDeptVo;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.core.exception.BadRequestException;
import com.xc.ezplan.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

/**
 * @author lixingchen
 * @date 2022/11/19
 * @description 部门管理
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门管理")
@RequiredArgsConstructor
public class AdminDeptController {

	private final AdminDeptService adminDeptService;

	private static final String ENTITY_NAME = "dept";

	@Log("新增部门")
	@ApiOperation("新增部门")
	@PostMapping
	@SaCheckPermission("dept.add")
	public EzResult<Object> createDept(@Validated @RequestBody AdminDeptVo adminDeptVo) {
		if (adminDeptVo.getId() != null) {
			throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
		}
		adminDeptService.createDept(adminDeptVo);
		return EzResult.ok();
	}

	@Log("删除部门")
	@ApiOperation("删除部门")
	@DeleteMapping
	@SaCheckPermission("dept.delete")
	public EzResult<Object> deleteDept(@RequestBody Set<Long> ids) {
		adminDeptService.deleteDept(ids);
		return EzResult.ok();
	}

	@Log("修改部门")
	@ApiOperation("修改部门")
	@PutMapping
	@SaCheckPermission("dept.update")
	public EzResult<Object> updateDept(@Validated @RequestBody AdminDeptVo adminDeptVo) {
		adminDeptService.updateDept(adminDeptVo);
		return EzResult.ok();
	}

	@ApiOperation("查询部门")
	@GetMapping
	public EzResult<Object> queryDept(@RequestParam(required = false) Long id) {
		return EzResult.ok(adminDeptService.queryDept(id));
	}

	@ApiOperation("根据条件分页查询部门")
	@PostMapping("/page")
	public EzResult<Object> pageQueryDept(@RequestBody BaseSearchBody<AdminDept> baseSearchBody) {
		return EzResult
				.ok(adminDeptService.pageQueryDept(baseSearchBody.getSearchCondition(), baseSearchBody.getPage()));
	}

	@ApiOperation("查询部门:根据ID获取同级与上级数据")
	@PostMapping("/superior")
	public EzResult<Object> getDeptSuperior(@RequestBody List<Long> ids) {
		return EzResult.ok();
	}

	@ApiOperation("导出部门数据")
	@GetMapping(value = "/download")
	public void exportDept(HttpServletResponse response, List<Long> ids) throws Exception {
		adminDeptService.exportDept(response, ids);
	}

}
